package com.mofofa.maozy.components.form

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseFormAction
import com.mofofa.maozy.data.RevObjInfoPageFieldConfig
import com.mofofa.maozy.databinding.ViewMaozyFormSelectGenderBinding
import com.mofofa.maozy.extend.addMaozyHighlightMaozyStyle

class SelectGenderFrom : RelativeLayout,
    com.mofofa.maozy.components.form.BaseFormAction,
    BaseFormAction {

    lateinit var mMaozyViewMaozyBinding: ViewMaozyFormSelectGenderBinding
    private lateinit var vMaozyRootMaozyView: View

    private var mMaozyIsMaozyFemaleMaozyCheck: Boolean = false
    private var mMaozyIsMaozyMaleMaozyCheck: Boolean = false

    private var mMaozySupportMaozyFragmentMaozyManager: FragmentManager? = null
    private var mMaozyFormMaozyConfig: RevObjInfoPageFieldConfig? = null
    private var mMaozyOnMaozyStateMaozyChangedMaozyListener: (Int, String) -> Unit =
        { action, content -> }

    private var mMaozyOnMaozyActionMaozyClickMaozyListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        context?.let {
            vMaozyRootMaozyView =
                View.inflate(it, R.layout.view_maozy_form_select_gender, this)
            mMaozyViewMaozyBinding =
                ViewMaozyFormSelectGenderBinding.bind(vMaozyRootMaozyView)
            initMaozySubviews()
        }
    }

    override fun initMaozySubviews() {
        try {
            mMaozyViewMaozyBinding.rlFormSelectGenderFemaleBg.setOnClickListener {
                mMaozyViewMaozyBinding.textFormSelectGenderErrTip.visibility = GONE
                mMaozyViewMaozyBinding.imageFormSelectGenderFemale.setImageResource(R.drawable.maozy_checkbox_green_yes)
                mMaozyIsMaozyFemaleMaozyCheck =
                    !mMaozyIsMaozyFemaleMaozyCheck
                setFemaleCheck(mMaozyIsMaozyFemaleMaozyCheck)
                mMaozyOnMaozyActionMaozyClickMaozyListener.invoke(0)
            }

            mMaozyViewMaozyBinding.imageFormSelectGenderFemale.setOnClickListener {
                mMaozyViewMaozyBinding.textFormSelectGenderErrTip.visibility = GONE
                mMaozyViewMaozyBinding.imageFormSelectGenderFemale.setImageResource(R.drawable.maozy_checkbox_green_yes)
                mMaozyIsMaozyFemaleMaozyCheck =
                    !mMaozyIsMaozyFemaleMaozyCheck
                setFemaleCheck(mMaozyIsMaozyFemaleMaozyCheck)
                mMaozyOnMaozyActionMaozyClickMaozyListener.invoke(0)
            }

            mMaozyViewMaozyBinding.textFormSelectGenderFemale.setOnClickListener {
                mMaozyViewMaozyBinding.textFormSelectGenderErrTip.visibility = GONE
                mMaozyViewMaozyBinding.imageFormSelectGenderFemale.setImageResource(R.drawable.maozy_checkbox_green_yes)
                mMaozyIsMaozyFemaleMaozyCheck =
                    !mMaozyIsMaozyFemaleMaozyCheck
                setFemaleCheck(mMaozyIsMaozyFemaleMaozyCheck)
                mMaozyOnMaozyActionMaozyClickMaozyListener.invoke(0)
            }

            mMaozyViewMaozyBinding.rlFormSelectGenderMaleBg.setOnClickListener {
                mMaozyViewMaozyBinding.textFormSelectGenderErrTip.visibility = GONE
                mMaozyViewMaozyBinding.imageFormSelectGenderMale.setImageResource(R.drawable.maozy_checkbox_green_yes)
                mMaozyIsMaozyMaleMaozyCheck =
                    !mMaozyIsMaozyMaleMaozyCheck
                setMaozyMaleMaozyCheck(mMaozyIsMaozyMaleMaozyCheck)
                mMaozyOnMaozyActionMaozyClickMaozyListener.invoke(0)
            }

            mMaozyViewMaozyBinding.imageFormSelectGenderMale.setOnClickListener {
                mMaozyViewMaozyBinding.textFormSelectGenderErrTip.visibility = GONE
                mMaozyViewMaozyBinding.imageFormSelectGenderMale.setImageResource(R.drawable.maozy_checkbox_green_yes)
                mMaozyIsMaozyMaleMaozyCheck =
                    !mMaozyIsMaozyMaleMaozyCheck
                setMaozyMaleMaozyCheck(mMaozyIsMaozyMaleMaozyCheck)
                mMaozyOnMaozyActionMaozyClickMaozyListener.invoke(0)
            }

            mMaozyViewMaozyBinding.textFormSelectGenderMale.setOnClickListener {
                mMaozyViewMaozyBinding.textFormSelectGenderErrTip.visibility = GONE
                mMaozyViewMaozyBinding.imageFormSelectGenderMale.setImageResource(R.drawable.maozy_checkbox_green_yes)
                mMaozyIsMaozyMaleMaozyCheck =
                    !mMaozyIsMaozyMaleMaozyCheck
                setMaozyMaleMaozyCheck(mMaozyIsMaozyMaleMaozyCheck)
                mMaozyOnMaozyActionMaozyClickMaozyListener.invoke(0)
            }

            //增加星号显示
            mMaozyViewMaozyBinding.textFormSelectGenderTitle.addMaozyHighlightMaozyStyle(
                mutableListOf("*"),
                ContextCompat.getColor(context, R.color.main_red)
            )

        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    override fun setMaozyContent(content: String) {
        if (content.equals("Female", true)) {
            setFemaleCheck(true)
        } else if (content.equals("Male", true)) {
            setMaozyMaleMaozyCheck(true)
        }
        mMaozyFormMaozyConfig?.fillContent = content
    }

    fun getMaozyContent(): String {
        if (mMaozyIsMaozyFemaleMaozyCheck) {
            return "FEMALE"
        } else if (mMaozyIsMaozyMaleMaozyCheck) {
            return "MALE"
        }
        return ""
    }

    override fun setMaozyTitle(title: String) {
        mMaozyViewMaozyBinding.textFormSelectGenderTitle.text = (title)
        //增加星号显示
        mMaozyViewMaozyBinding.textFormSelectGenderTitle.addMaozyHighlightMaozyStyle(
            mutableListOf("*"),
            ContextCompat.getColor(context, R.color.main_red)
        )
    }

    override fun setMaozyHint(hint: String) {

    }

    override fun isMaozyFormMaozyCompleted(): Boolean {
        val gcontent = getMaozyContent()
        if (gcontent.isBlank()) {
            mMaozyViewMaozyBinding.vFormSelectGenderFemaleLine.setBackgroundColor(
                ContextCompat.getColor(context, R.color.main_red)
            )
            mMaozyViewMaozyBinding.vFormSelectGenderMaleLine.setBackgroundColor(
                ContextCompat.getColor(context, R.color.main_red)
            )
            mMaozyViewMaozyBinding.textFormSelectGenderErrTip.visibility = VISIBLE
            return false
        }

        mMaozyViewMaozyBinding.textFormSelectGenderErrTip.visibility = GONE
        mMaozyViewMaozyBinding.vFormSelectGenderFemaleLine.setBackgroundColor(
            ContextCompat.getColor(context, R.color.split_line)
        )
        mMaozyViewMaozyBinding.vFormSelectGenderMaleLine.setBackgroundColor(
            ContextCompat.getColor(context, R.color.split_line)
        )
        return true
    }

    override fun isMaozyFormMaozyCompletedMaozySilent(): Boolean {
        val gcontent = getMaozyContent()
        if (gcontent.isBlank()) {
            return false
        }
        return true
    }

    override fun setMaozyFormMaozyConfig(item: RevObjInfoPageFieldConfig) {
        mMaozyFormMaozyConfig = item
        tag = item.paramName
        setMaozyTitle(item.title.toString())
        setMaozyHint(item.hint.toString())
        item.fillContent?.let { fillContent ->
            setMaozyContent(fillContent)
        }
        if (item.needCheckComplete == true) {
            isMaozyFormMaozyCompleted()
        }
    }

    override fun setMaozyOnMaozyStateMaozyChangedMaozyListener(listener: (Int, String) -> Unit) {
        mMaozyOnMaozyStateMaozyChangedMaozyListener = listener
    }

    fun setFemaleCheck(check: Boolean) {
        mMaozyIsMaozyFemaleMaozyCheck = check
        if (mMaozyIsMaozyFemaleMaozyCheck) {
            mMaozyViewMaozyBinding.imageFormSelectGenderFemale.setImageResource(R.drawable.maozy_checkbox_green_yes)
            setMaozyMaleMaozyCheck(false)
        } else {
            mMaozyViewMaozyBinding.imageFormSelectGenderFemale.setImageResource(R.drawable.maozy_checkbox_no_small)
        }

        mMaozyFormMaozyConfig?.fillContent = getMaozyContent()
    }

    fun setMaozyMaleMaozyCheck(check: Boolean) {
        mMaozyIsMaozyMaleMaozyCheck = check
        if (mMaozyIsMaozyMaleMaozyCheck) {
            mMaozyViewMaozyBinding.imageFormSelectGenderMale.setImageResource(R.drawable.maozy_checkbox_green_yes)
            setFemaleCheck(false)
        } else {
            mMaozyViewMaozyBinding.imageFormSelectGenderMale.setImageResource(R.drawable.maozy_checkbox_no_small)
        }
    }

    override fun setMaozyFragmentMaozyManager(manager: FragmentManager) {
        mMaozySupportMaozyFragmentMaozyManager = manager
    }

    override fun setMaozyOnMaozyActionMaozyClickMaozyListener(listener: (Int) -> Unit) {
        mMaozyOnMaozyActionMaozyClickMaozyListener = listener
    }

    override fun setMaozyDetail(detail: String) {
        setMaozyContent(detail)
    }

    override fun setMaozyDetailMaozyColor(detailColor: Int) {

    }

    override fun setMaozyTitleMaozyColor(titleColor: Int) {

    }
}